{% extends './layout.html' %} {% block title %} {{ title }} {% endblock %}{% block crumbs %}
<a href="/">首页</a>
<a><cite>小说管理</cite></a>
{% endblock %} {% block body %}
<div class="layout_head">
	<button type="button" class="layui-btn layui-btn-sm" id="addStoryType"><i class="layui-icon">&#xe608;</i> 新增类型</button>
	<button type="button" class="layui-btn layui-btn-sm" id="sortStoryType">更新类型排序</button>
</div>
<div class="layout_cont">
	<table class="layui-table">
		<colgroup>
			<col width="120">
			<col>
			<col width="160">
			<col width="100">
		</colgroup>
		<thead>
			<tr>
				<th>类型编号</th>
				<th>类型名称</th>
				<th>类型排序</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody id="storyTypeList">
			{% for index,item in list %}
			<tr>
				<td>{{ index+1 }}</td>
				<td>{{ item.typename }}</td>
				<td style="padding: 5px 10px;"><input type="text" name="sort" class="layui-input" style="height: 30px; width: 50px; text-align: center; padding: 0;" data-id="{{item.id}}" data-name="{{item.typename}}" value="{{item.typesort}}" /></td>
				<td>
					<div class="layui-btn-group">
						<button type="button" class="layui-btn layui-btn-xs layui-btn-warm" data-type="edit" data-name="{{item.typename}}" data-id="{{item.id}}">编辑</button>
						<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" data-type="del" data-id="{{item.id}}">删除</button>
					</div>
				</td>
			</tr>
			{% endfor %}
		</tbody>
	</table>
</div>
{% endblock %} {% block scripts %}
<script type="text/javascript">
	layui.use(['layer'], function() {
		var $ = layui.jquery;
		var layer = layui.layer;
		$('#addStoryType').on('click', function() {
			layer.prompt({
				btnAlign: 'c',
				title: '增加小说分类'
			}, function(value, index, elem) {
				$.post('/storytype/create', {
					typename: value
				}, function(data) {
					layer.close(index);
					window.location.reload();
				});
			});
		});
		$('#sortStoryType').on('click', function() {
			var arr = [];
			$('input[name="sort"]').each(function() {
				arr.push({
					id: $(this).data('id'),
					typename: $(this).data('name'),
					typesort: $(this).val()
				});
			});
			$.post('/storytype/sort', {
				sorts: arr
			}, function(data) {
				window.location.reload();
			});
		});
		$('#storyTypeList').on('click', 'button.layui-btn', function() {
			var id = $(this).data('id');
			var type = $(this).data('type');
			switch(type) {
				case 'edit':
					var tname = $(this).data('name');
					layer.prompt({
						btnAlign: 'c',
						value: tname,
						title: '增加小说分类'
					}, function(value, index, elem) {
						if(tname !== value) {
							$.post('/storytype/update', {
								id: id,
								name: value
							}, function(data) {
								layer.close(index);
								window.location.reload();
							});
						} else {
							layer.close(index);
						}
					});
				break;
				case 'del':
					layer.confirm('是否删除当前管理员？', {
						btnAlign: 'c',
						title: '删除确认'
					}, function(index) {
						$.post('/storytype/delete', {
							id: id
						}, function(data) {
							layer.close(index);
							window.location.reload();
						});
					});
				break;
			}
		});
	});
</script>
{% endblock %}